//
//  VideoShowTableViewCell.m
//  HaHa
//
//  Created by 元虎的mac on 15/9/24.
//  Copyright (c) 2015年 吉亚平. All rights reserved.
//

#import "VideoShowTableViewCell.h"

@implementation VideoShowTableViewCell



- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        self.selectionStyle = UITableViewCellSelectionStyleNone;
        [self AddContentViews];
    }
    return self;
}

- (void)AddContentViews{
    
    //作者头像
    self.authorPhotoImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, 10, 30, 30)];
    self.authorPhotoImage.layer.masksToBounds = YES;
    self.authorPhotoImage.layer.cornerRadius = _authorPhotoImage.height/2;
//    self.authorPhotoImage.backgroundColor = KArcColor;
    [self.contentView addSubview:self.authorPhotoImage];
    
    //作者昵称
    self.authorName = [[UILabel alloc] initWithFrame:CGRectMake(_authorPhotoImage.X_width+10, 10, kScreenWdth-10-10-100-_authorPhotoImage.X_width, 30)];
//    self.authorName.backgroundColor = KArcColor;
    self.authorName.textAlignment = NSTextAlignmentLeft;
    [self.contentView addSubview:self.authorName];
    
    //标题
    self.detailedLable = [[UILabel alloc] initWithFrame:CGRectMake(10, _authorPhotoImage.Y_height + 20, kScreenWdth-30, 21)];
    self.detailedLable.numberOfLines = 0;
    self.detailedLable.textAlignment = NSTextAlignmentLeft;
//    self.detailedLable.backgroundColor = KArcColor;
    [self.contentView addSubview:self.detailedLable];
    
    //缩略图
    self.thumbnailImage = [[UIImageView alloc] initWithFrame:CGRectMake(10, _detailedLable.Y_height + 20, kScreenWdth-30, 0)];
//    self.thumbnailImage.backgroundColor = KArcColor;
    [self.contentView addSubview:self.thumbnailImage];
    
    //评论背景
    self.commentBackView = [[UIView alloc] initWithFrame:CGRectMake(10, _thumbnailImage.Y_height+10, kScreenWdth-30, 0)];
    self.commentBackView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    [self.contentView addSubview:self.commentBackView];
    
    //神评论
    UILabel *NBcomment = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 30, 12)];
    NBcomment.text = @"神评论";
    NBcomment.font = [UIFont systemFontOfSize:10];
    NBcomment.textColor = [UIColor whiteColor];
    NBcomment.textAlignment = NSTextAlignmentCenter;
    NBcomment.backgroundColor = [UIColor brownColor];
    [self.commentBackView addSubview:NBcomment];
    
    //神评论图像
    self.commentSPhoto = [[UIImageView alloc] initWithFrame:CGRectMake(10, NBcomment.Y_height+10, 20, 20)];
    self.commentSPhoto.layer.masksToBounds = YES;
    self.commentSPhoto.layer.cornerRadius = 10;
    self.commentSPhoto.backgroundColor = KArcColor;
    [self.commentBackView addSubview:self.commentSPhoto];
    
    //神评论作者
    self.commentName = [[UILabel alloc] initWithFrame:CGRectMake(40, _commentSPhoto.Y, kScreenWdth-80, 20)];
    self.commentName.textAlignment = NSTextAlignmentLeft;
    [self.commentBackView addSubview:self.commentName];
    

    self.commentDetaile = [[UILabel alloc] initWithFrame:CGRectMake(40, _commentName.Y_height+10, kScreenWdth-80, 20)];
    self.commentDetaile.numberOfLines = 0;
    [self.commentBackView addSubview:self.commentDetaile];
    
    _playCount = [[UILabel alloc] initWithFrame:CGRectMake(_authorName.X_width +5, _authorName.Y, 80, 30)];
    _playCount.textColor = KgrayColor;
    _playCount.text = @"2323次播放";
    _playCount.textAlignment = NSTextAlignmentRight;
    _playCount.font = [UIFont systemFontOfSize:11];
    [self addSubview:_playCount];
    
    
    _buttonBackView = [[UIView alloc] initWithFrame:CGRectMake(15, _commentBackView.Y_height+5, kScreenWdth-40, 30)];
//    _buttonBackView.backgroundColor = [UIColor colorWithRed:0.9 green:0.9 blue:0.9 alpha:1];
    
    UIImageView *praise = [[UIImageView alloc] initWithFrame:CGRectMake(40,0, 30, 30)];
    praise.image = [UIImage imageNamed:@"praise"];
    [self.buttonBackView addSubview:praise];
    _praiseCount = [[UILabel alloc] initWithFrame:CGRectMake(65, 0,40 , 30)];
    _praiseCount.textColor = KgrayColor;
    _praiseCount.font = [UIFont systemFontOfSize:11];
    [_buttonBackView addSubview:_praiseCount];
    
    UIImageView *buried = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWdth/2-30, 0, 30, 30)];
    buried.image = [UIImage imageNamed:@"buried"];
    [_buttonBackView addSubview:buried];
    
    _buried = [[UILabel alloc] initWithFrame:CGRectMake(buried.X_width-5, 0, 40, 30)];
    _buried.font = [UIFont systemFontOfSize:11];
    _buried.textColor = KgrayColor;
    [self.buttonBackView addSubview:_buried];
    
    UIImageView *Comment = [[UIImageView alloc] initWithFrame:CGRectMake(kScreenWdth-100, 0, 30, 30)];
    Comment.image = [UIImage imageNamed:@"Comment"];
    [self.buttonBackView addSubview:Comment];
    
    self.commentCount = [[UILabel alloc] initWithFrame:CGRectMake(Comment.X_width-5, 0, 40, 30)];
    _commentCount.font = [UIFont systemFontOfSize:11];
    _commentCount.textColor = KgrayColor;
    [self.buttonBackView addSubview:self.commentCount];
    
    
    
    
    [self.contentView addSubview:_buttonBackView];
    
}


//控件内容设置 内涵段子mode
- (void)setContentForHMode:(HotVideoMode *)Mode{
    
    [self.authorPhotoImage sd_setImageWithURL:[NSURL URLWithString:[Mode.user objectForKey:@"avatar_url"]] placeholderImage:nil];
    self.authorName.text = [Mode.user objectForKey:@"name"];
    self.detailedLable.text = Mode.text.length == 0 ? @"还没有标题" : Mode.text;
    
    NSArray *urlDicArr = [Mode.large_cover objectForKey:@"url_list"];
    NSDictionary *urlDic = [urlDicArr firstObject];
    NSString *url = [urlDic objectForKey:@"url"];
    [self.thumbnailImage sd_setImageWithURL:[NSURL URLWithString:url] placeholderImage:[UIImage imageNamed:@"sdsdsds.jpg"]];
    

        NSDictionary *comment = [Mode.comments firstObject];
        [self.commentSPhoto sd_setImageWithURL:[NSURL URLWithString:[comment objectForKey:@"avatar_url"]] placeholderImage:[UIImage imageNamed:@"sdsdsds.jpg"]];
        self.commentDetaile.text = [comment objectForKey:@"text"];
        self.commentName.text = [comment objectForKey:@"user_name"];
    self.playCount.text = [NSString stringWithFormat:@"%ld次播放",Mode.play_count];
    
    
    self.praiseCount.text = [NSString stringWithFormat:@"%ld",Mode.favorite_count];
    self.buried.text = [NSString stringWithFormat:@"%ld",Mode.bury_count];
    self.commentCount.text = [NSString stringWithFormat:@"%ld",Mode.comment_count];
    
    
    
    [self setAllFrame:Mode];
}
- (void)setAllFrame:(HotVideoMode *)mode{
    
    
    //标题frame
    CGFloat TitleHeight = [self YYHreturnStringHeight:mode.text Width:kScreenWdth-30 Font:17];
    if (TitleHeight==0) {
        TitleHeight = 21;
    }
    _detailedLable.frame = CGRectMake(_detailedLable.X, _detailedLable.Y, _detailedLable.width, TitleHeight);
    
    
    
    //缩略图frame
    NSInteger videoWdith = [mode.video_width integerValue];
    //将要返回的视频高度
    CGFloat returnVideoHeight = [mode.video_height floatValue];
    CGFloat returnVideoWidth = [mode.video_width floatValue];    if (videoWdith > kScreenWdth-30) {
        returnVideoWidth = kScreenWdth -30;
        //计算表视图的宽度与视频宽度的比例
        CGFloat beishu = videoWdith / (kScreenWdth-30);
        returnVideoHeight = [mode.video_height integerValue]/beishu;
    }
    
    if (returnVideoHeight == 0) {
        returnVideoWidth = kScreenWdth-30;
        returnVideoHeight = kScreenWdth-30;
    }
    
    self.thumbnailImage.frame = CGRectMake((kScreenWdth-10)/2-returnVideoWidth/2, _detailedLable.Y_height + 20, returnVideoWidth, returnVideoHeight);
    
    //评论frame
    CGFloat commentsHeight = [self YYHreturnStringHeight:_commentDetaile.text Width:_commentDetaile.width Font:17];
    if (mode.comments.count==0) {
        _commentBackView.hidden = YES;
         _commentBackView.frame = CGRectMake(_commentBackView.X, _thumbnailImage.Y_height, _commentBackView.width,0);
    }else{
        _commentBackView.hidden = NO;
         _commentBackView.frame = CGRectMake(_commentBackView.X, _thumbnailImage.Y_height+10, _commentBackView.width,commentsHeight+60);
    }
    
    _buttonBackView.frame = CGRectMake(0, _commentBackView.Y_height+5, kScreenWdth-10, 30);
    
    _commentDetaile.frame = CGRectMake(_commentDetaile.X, _commentDetaile.Y, _commentDetaile.width, commentsHeight);
    
    
}





//计算高度
- (CGFloat)YYHreturnStringHeight:(NSString *)string Width:(CGFloat)Width Font:(int)font{
    
    CGRect frame = [string boundingRectWithSize:CGSizeMake(Width, 10000) options:(NSStringDrawingUsesLineFragmentOrigin) attributes:[NSDictionary dictionaryWithObject:[UIFont systemFontOfSize:font] forKey:NSFontAttributeName] context:nil];
    return frame.size.height;
}



- (void)awakeFromNib {
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    
    [super setSelected:selected animated:animated];
}

@end
